const path = require('path');
const alias = require('@rollup/plugin-alias');
const replace = require('@rollup/plugin-replace');
const nodeResolve = require('@rollup/plugin-node-resolve').nodeResolve;
const commonjs = require('@rollup/plugin-commonjs');
const babel = require('@rollup/plugin-babel').babel;
const terser = require('rollup-plugin-terser').terser;

const isProduction = process.env.NODE_ENV === 'production';

const plugins = [
  alias({
    entries: [{
        find: '@core',
        replacement: path.resolve(__dirname, 'src/core'),
      },
      {
        find: '@shared',
        replacement: path.resolve(__dirname, 'src/shared'),
      },
    ],
  }),
  replace({
    ENV: JSON.stringify(process.env.NODE_ENV || 'development'),
  }),
  nodeResolve(),
  commonjs(),
  babel({
    babelHelpers: 'bundled',
  }),
];

if (isProduction) {
  plugins.push(terser());
}

const projectPath = '../../projects/hello-uniapp/node_modules/vni/dist/index.js';

module.exports = {
  input: 'src/scripts/index.js',
  output: {
    file: isProduction ? 'dist/index.js' : projectPath,
    format: 'cjs',
  },
  plugins,
};
